/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ereconquista.dao;

import com.ereconquista.modelo.Pregunta;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 * @author Claudio
 */
public class PreguntaDAO extends BaseDAO<Pregunta> implements Serializable {

    public Pregunta getPreguntaByLink(String codigoPregunta) {
         
        EntityManager em = null;
        String sQuery = "";
        try {
           sQuery = "SELECT p FROM Pregunta p WHERE p.codigo = :link ";
                        
           em = getEntityManager();           
           Query q = em.createQuery(sQuery);

           q.setParameter("link", codigoPregunta);
          
           return (Pregunta) q.getSingleResult();
       } catch (Exception e) {
           
           e.printStackTrace();           
           System.err.print("No se ejecuto queryList: "+sQuery );
           return null;
       }finally {
            if (em != null) {
                em.close();
            }
        }  
    }

    public List<Pregunta> getPreguntasByUsuario(Integer id) {
        
        //System.err.println("id: "+ id);
        
        EntityManager em = null;
        String sQuery = "";
        try {
           sQuery = "SELECT p FROM Pregunta p WHERE p.publicacion.usuario.id = :idUsuario "                   
                   + "ORDER BY p.fecha DESC";
                        
           em = getEntityManager();           
           Query q = em.createQuery(sQuery);

           q.setParameter("idUsuario", id);
           
           q.setMaxResults(30);
           
           return q.getResultList();
       } catch (Exception e) {
           
           e.printStackTrace();           
           System.err.print("No se ejecuto queryList: "+sQuery );
           return null;
       }finally {
            if (em != null) {
                em.close();
            }
        }  
    }

    public List<Pregunta>  getPreguntasByPublicacion(Integer id) {
        EntityManager em = null;
        String sQuery = "";
        try {
           sQuery = "SELECT p FROM Pregunta p WHERE p.publicacion.id = :idPublicacion "
                   + "AND p.estado = 'R' "                   
                   + "ORDER BY p.fecha DESC";
                        
           em = getEntityManager();           
           Query q = em.createQuery(sQuery);

           q.setParameter("idPublicacion", id);
           
           return q.getResultList();
       } catch (Exception e) {
           
           e.printStackTrace();           
           System.err.print("No se ejecuto queryList: "+sQuery );
           return new ArrayList<Pregunta>();
       }finally {
            if (em != null) {
                em.close();
            }
        }  
    }
}
